package cn.agiledata.bank.linkbank.impl.abc;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import com.hitrust.trustpay.client.TrxException;
import com.hitrust.trustpay.client.b2c.PaymentRequest;
import com.hitrust.trustpay.client.b2c.PaymentResult;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.linkbank.logic.payment.BankPayResponse;

/**
 * 中国农业银行网上银行支付响应
 * 
 * @author 马贤波
 * 
 */
public final class AbcNetPayResponse implements BankPayResponse {
    private String message;

    private PaymentResult paymentResult;

    public BigDecimal getAmount() {
        try {
            return new BigDecimal(
                    getMessage(AbcNetConstant.REQUEST_PAY_AMOUNT));
        } catch (Exception e) {
            return null;
        }
    }

    public String getReturnMessage() {
        return this.paymentResult.getErrorMessage();
    }

    public String getMessage(String key) {
        return this.paymentResult.getValue(key);
    }

    public int getResponseMode() {
        String notifyType = getMessage(AbcNetConstant.NOTIFY_TYPE);
        if (PaymentRequest.NOTIFY_TYPE_URL.equals(notifyType)) {
            return Constant.RESPONSE_MODE_PAGE;
        }
        if (PaymentRequest.NOTIFY_TYPE_SERVER.equals(notifyType)) {
            return Constant.RESPONSE_MODE_BACKGROUND;
        }
        return Constant.RESPONSE_MODE_NEVER;
    }

    public String getReturnCode() {
        return this.paymentResult.getReturnCode();
    }

    public boolean isSuccessful() {
        return this.paymentResult.isSuccess();
    }

    public void setMessages(Map messages) {
        String message = (String) messages.get(AbcNetConstant.MESSAGE);
        try {
            this.paymentResult = new PaymentResult(message);
            this.message = message;
        } catch (TrxException e) {
            this.paymentResult = null;
        }
    }

    public BankPayResponse copy() {
        AbcNetPayResponse response = new AbcNetPayResponse();
        if (this.paymentResult == null)
            return response;
        try {
            response.paymentResult = new PaymentResult(this.message);
        } catch (TrxException e) {
        }
        return response;
    }

    public String getOrderId() {
        return getMessage("OrderNo");
    }

    public Date getDate() {
        String dateValue = getMessage("HostDate");
        String timeValue = getMessage("HostTime");
        return DateUtil.string2Date(dateValue + " " + timeValue,
                AbcNetConstant.DATE_FORMAT + " "
                        + AbcNetConstant.TIME_FORMAT);
    }

    /**
     * 
     * @return 交易批次号
     */
    public String getBatchNo() {
        return getMessage("BatchNo");
    }

    /**
     * 
     * @return 交易凭证号（交易对账时使用）
     */
    public String getVouncherNo() {
        return getMessage("VoucherNo");
    }

    public final boolean isAmountValidatable() {
        return false;
    }

}
